package io.gitee.zhangbinhub.admin.oauth.bus.listener

import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.common.base.BaseCloudEventHandler
import io.gitee.zhangbinhub.admin.common.constant.BusEventConstant
import io.gitee.zhangbinhub.admin.common.event.ReloadDataBusEvent
import io.gitee.zhangbinhub.admin.oauth.conf.AcpOauthServerAutoConfiguration
import io.gitee.zhangbinhub.admin.oauth.constant.BusEventMessage
import io.gitee.zhangbinhub.admin.oauth.service.RuntimeConfigService
import org.noear.solon.cloud.annotation.CloudEvent

/**
 * @since JDK 11
 */
@CloudEvent(BusEventConstant.RELOAD_DATA_BUS_EVENT_TOPIC)
open class ReloadDataEventListener(
    private val logAdapter: LogAdapter,
    private val acpOauthServerAutoConfiguration: AcpOauthServerAutoConfiguration,
    private val runtimeConfigService: RuntimeConfigService
) : BaseCloudEventHandler(logAdapter) {
    override fun doHandle(reloadDataBusEvent: ReloadDataBusEvent) {
        when (reloadDataBusEvent.content.message) {
            BusEventMessage.refreshApplication -> {
                logAdapter.info("收到刷新application消息")
                acpOauthServerAutoConfiguration.loadClientInfo()
                logAdapter.info("刷新application完成")
            }

            BusEventMessage.refreshRuntime -> {
                logAdapter.info("收到刷新runtime参数消息")
                runtimeConfigService.loadRuntimeConfig()
                logAdapter.info("刷新runtime参数完成")
            }
        }
    }
}